<?php

/*
Copyright 2009-2011 Sam Weiss
All Rights Reserved.

This file is part of Spark/Plug.

Spark/Plug is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('spark/plug'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

// -----------------------------------------------------------------------------

class _SparkSessionStore_native extends SparkSessionStore
{

	// --------------------------------------------------------------------------

	public function __construct($params)
	{
		parent::__construct($params);
	}

	// --------------------------------------------------------------------------

	// Public Methods

	// --------------------------------------------------------------------------

	public function start()
	{
		if (!isset($_SESSION))
		{
			session_name($this->_name);
			@session_set_cookie_params(0, $this->_path, $this->_domain, false, true);
			session_start();
		}
	}

	// --------------------------------------------------------------------------

	public function get($key, $default = NULL)
	{
		return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
	}

	// --------------------------------------------------------------------------

	public function set($key, $val)
	{
		return $_SESSION[$key] = $val;
	}

	// --------------------------------------------------------------------------

	public function remove($key)
	{
		$val = @$_SESSION[$key];
		unset($_SESSION[$key]);
		return $val;
	}

	// --------------------------------------------------------------------------

	public function clear()
	{
		$_SESSION = array();
	}

	// --------------------------------------------------------------------------

	public function destroy()
	{
		unset($_SESSION);				// session cannot be revived during this http request!

		if (isset($_COOKIE[session_name()]))
		{
			@setcookie(session_name(), '', time()-42000, $this->_path, $this->_domain, false, true);
		}

		session_destroy();
		
		parent::destruct();
	}

	// --------------------------------------------------------------------------
}
